﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>北京时间</title>
    <script type="text/javascript" src="/js/layout.js"></script>
    <script type="text/javascript">
        function setLocalTime() {
            var time = new Date();
            var year = time.getFullYear();
            var month = (time.getMonth() + 1).toPaddedString(2, 0);
            var date = time.getDate().toPaddedString(2, 0);
            var hour = time.getHours().toPaddedString(2, 0);
            var min = time.getMinutes().toPaddedString(2, 0);
            var sec = time.getSeconds().toPaddedString(2, 0);
            $('#local').html(year + '年' + month + '月' + date + '日 ' + hour + ":" + min + ":" + sec);
        }

        function setWebTime() {
            var time = getWebTime();
            if (time != null) {
                var year = time.getFullYear();
                var month = time.getMonth().toPaddedString(2, 0);
                var date = time.getDate().toPaddedString(2, 0);
                var hour = time.getHours().toPaddedString(2, 0);
                var min = time.getMinutes().toPaddedString(2, 0);
                var sec = time.getSeconds().toPaddedString(2, 0);
                $('#web').html(year + '年' + month + '月' + date + '日 ' + hour + ":" + min + ":" + sec);
            } else {
                $('#web').html('无法连接网络。');
            }
        }
        function getWebTime() {
            var ret = unescape(_eval('ajax("GET","http://www.time.ac.cn/timeflash.asp?user=flash")'));
            if (ret.length > 0) {
                var doc = $.parseXML(ret);
                var $xml = $(doc);
                var year = $xml.find('year').text();
                var month = $xml.find('month').text();
                var day = $xml.find('day').text();
                var hour = $xml.find('hour').text();
                var minite = $xml.find('minite').text();
                var second = $xml.find('second').text();
                return new Date(year, month, day, hour, minite, second);
            }
            return null;
        }

        $(function () {
            setLocalTime();
            setInterval('setLocalTime()', 500);
            setWebTime();
            setInterval('setWebTime()', 1000);

            $('#sync').click(function () {
                var t = getWebTime();
                if (t == null) {
                    alert('无法连接网络。');
                    return;
                }
                var time = t.getFullYear() + '-' + t.getMonth() + '-' + t.getDate() + ' ' + t.getHours() + ':' + t.getMinutes() + ':' + t.getSeconds();
                //_eval('setSystemTime("' + time + '")');
                document.location.reload();
            });
        });
    </script>
</head>
<body>
    <h1>
        网络时间：<span id="web"> </span>
    </h1>
    <h1>
        本地时间：<span id="local"> </span>
    </h1>
    <input id="sync" type="button" value="同步网络时间" />
</body>
</html>
